package com.photoeditor.demo.ui.widget.image.watermark;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.common.base.ComponentContext;
import com.common.base.ui.widget.ripple.RippleImageView;
import com.photoeditor.R;
import com.photoeditor.demo.util.KeyBoardHelper;

import static com.photoeditor.demo.ui.widget.image.watermark.WatermarkManager.DEFAULT_CUSTOM_TEXT;
import static com.photoeditor.demo.ui.widget.image.watermark.WatermarkManager.DEFAULT_TEXT_TYPE_FACE_PATH;

/**
 * 水印文字修改弹窗
 * Created by edward on 17-7-5.
 */

public class CustomWatermarkTextDialog extends Dialog implements View.OnClickListener {
    private EditText mEditText;
    private RippleImageView mFinishBtn;
    private OnInputFinishListener mOnInputFinishListener;

    public CustomWatermarkTextDialog(Context context) {
        super(context, R.style.dialog_fullscreen);
        setContentView(R.layout.dialog_watermark_edit_text);

        mEditText = (EditText) findViewById(R.id.et_text);
        mEditText.setTypeface(Typeface.createFromAsset(context.getAssets(), DEFAULT_TEXT_TYPE_FACE_PATH));
        mEditText.requestFocus();
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        mFinishBtn = (RippleImageView) findViewById(R.id.riv_text_finish);
        mFinishBtn.setOnClickListener(this);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                KeyBoardHelper.hideKeyBoard(ComponentContext.getContext(), mEditText);
            }
        });
    }

    public void setEditTextString(String text) {
        if (text == null) {
            return;
        }
        mEditText.setText(text);
        if (mEditText.getText() != null) {
            mEditText.setSelection(mEditText.getText().length());
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);

        KeyBoardHelper.showKeyBoardWithoutDelay(ComponentContext.getContext(), mEditText);
        mEditText.requestFocus();
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
    }

    @Override
    public void onClick(View v) {
        if (v == mFinishBtn) {
            if (mOnInputFinishListener != null) {
                String text = mEditText.getText().toString();
                mOnInputFinishListener.onInputFinish(TextUtils.isEmpty(text) ? DEFAULT_CUSTOM_TEXT : text);
            }
        }
    }

    public void setOnInputFinishListener(OnInputFinishListener onInputFinishListener) {
        mOnInputFinishListener = onInputFinishListener;
    }

    public interface OnInputFinishListener {
        void onInputFinish(String input);
    }

}
